view.canvas = {
    
    renderTagCloud : function(data) {
    	
			var html = Mustache.to_html(template.canvas.tag_cloud, {"items" : data});
			
			$("#section-tagcloud").html(html);
			
			$("a.tag-cloud").bind("click", function() {
				controller.canvas.listSelectedWishes("tagged_" + this.rel);
			});
			
			$("a.lnk-to-add").bind("click", function() {

				$("#tabs").tabs("select", 1);
			});
    },
		
	renderWishAdd : function(params) {
	    	
			$("#wishlist").hide();
			
			if (utils.empty(params)) params = {};
		
	        params.reasons = config.reasons;
	        params.categories = config.categories;
	        params.img_url = config.default_image;
	        
	        var content = Mustache.to_html(template.canvas.wish_add, params);
	    	$("#wish_add").html(content);
	    	
			$('#form-add-cancel').bind('click', function() {
				$('#form-add').slideUp();
				$("#wishlist").show();
			});
			
			$('#form-add-submit').bind('click', function() {
				controller.canvas.addWish(function() {
					$('#form-add').slideUp();
					$("#wishlist").show();
					view.common.showMessage(utils.getText("notice.wish_added"));
				});
			});
			
			$('#img-add-show').bind('click', function() {
				
				var query = $("#form-add-title").val();
				
				if (query != "") {
					$("#img-add-query").attr({value : query});
					view.common.searchImages(query, "add");
				}
				$("#img-add-panel").slideToggle();
			});
			
			$('#img-add-search').bind('click', function() {
				var query = $('#img-add-query').val();
				view.common.searchImages(query, "add");
			});
			
			$('#form-add').slideDown();
    },
		
	renderWishEdit : function(data) {

			data.reasons = config.reasons;
			data.categories = config.categories;
			
			var content = Mustache.to_html(template.canvas.wish_edit, data);
			
			$("#wish_edit").html(content);
			
			$('#form-edit-reason option[value=' + data.reason_id + ']').attr('selected', 'selected');
			$('#form-edit-category option[value=' + data.category_id + ']').attr('selected', 'selected');
			
			$('#form-edit').slideDown();
			$("#wishlist").hide();
			
			$('#form-edit-cancel').bind('click', function() {
				$('#form-edit').slideUp();
				$("#wishlist").show();
			});
			
			$('#form-edit-submit').bind('click', function() {
				controller.canvas.editWish(data.wish_id, function() {
					
					$('#form-edit').slideUp();
					$("#wishlist").show();
					view.common.showMessage(utils.getText("notice.wish_edited"));
				});
			});
			
			$('#form-edit-remove').bind('click', function() {
				
				view.common.showConfirmDialog(utils.getText("confirm.wish_remove"), function() {
					
					controller.canvas.removeWish(data.wish_id);
					$('#form-edit').slideUp();
					$("#wishlist").show();
				});
			});
			
			$('#img-edit-show').bind('click', function() {
				var query = $("#form-edit-title").val();
				
				if (query != "") {
					$("#img-edit-query").attr({value : query});
					view.common.searchImages(query, "edit");
				}
				$("#img-edit-panel").slideToggle();
			});
			
			$('#img-edit-search').bind('click', function() {
				var query = $('#img-edit-query').val();
				view.common.searchImages(query, "edit");
			});
		},
		
	renderReserves : function(data) {
			
		$("#reserves-list").hide();
		
		data.show_prev = data.page > 1;
		data.show_next = data.page < data.pages;
		
		var html = Mustache.to_html(template.canvas.reserves_list, data);
		
		$("#reserves-list").html(html);
		
		$('a.link-reserve-remove').bind('click', function() {
			
			controller.canvas.removeReserve(this.rel);
		});
		
        $("#reserve-pager-next").bind('click', function(event) {

        	controller.canvas.listReserves("next");
        });
        
        $("#reserve-pager-prev").bind('click', function(event) {
        	
        	controller.canvas.listReserves("prev");
        });
        
        $('a.link-wish-show').unbind().bind('click', function(event) {
        	controller.canvas.showWish(this.rel)
        });
		
		$("#reserves-list").slideDown("slow");
	},
	
	renderFriendWishes : function(data) {
		
		$("#wishlist-friend").hide();

		data.show_prev = data.page > 1;
		data.show_next = data.page < data.pages;
		
		var html = Mustache.to_html(template.canvas.wish_list_friend, data);
		
		$("#wishlist-friend").html(html);
		
		$('a.link-wish-reserve').bind('click', function() {
			controller.canvas.addReserve(data.owner_id, this.rel);
		});
		
        $("#friend-pager-next").bind('click', function(event) {

        	controller.canvas.listFriendWishes(data.owner_id, "next");
        });
        
        $("#friend-pager-prev").bind('click', function(event) {
        	
        	controller.canvas.listFriendWishes(data.owner_id, "prev");
        });
		
        $('a.link-wish-show').unbind().bind('click', function(event) {
        	controller.canvas.showWish(this.rel)
        });
		
		$("#wishlist-friend").slideDown("slow", gadgets.window.adjustHeight);
	},
			
	renderOwnWishes : function(data) {
		
		$("#wishlist-own").hide();
		
		data.show_prev = data.page > 1;
		data.show_next = data.page < data.pages;
		data.uid = user_uid;

		var html = Mustache.to_html(template.canvas.wish_list_own, data);

        $("#wishlist-own").html(html);
        
		$('a.link-show-add').bind('click', function() {

			view.canvas.renderWishAdd();
	  	});
        
        $('a.link-wish-edit').bind('click', function() {
        	
    		controller.canvas.showWishEdit(this.rel);
        });
        
        $('a.link-wish-show').unbind().bind('click', function(event) {
        	controller.canvas.showWish(this.rel)
        });
        
        $("#link-pager-next").bind('click', function(event) {
        	
        	controller.canvas.listOwnWishes("next");
        });
        
        $("#link-pager-prev").bind('click', function(event) {
        	controller.canvas.listOwnWishes("prev");
        });
		
        $("#wishlist-own").slideDown("slow");
        
        gadgets.window.adjustHeight(600);
	},
	
	renderSelectedWishes : function(data) {
		
		$("#section-selection").hide();
				
		data.show_prev = data.page > 1;
		data.show_next = data.page < data.pages;
		
		var html = Mustache.to_html(template.canvas.selection_list, data);
		
		$("#section-selection").html(html);
		
		$('a.link-wish-show').unbind().bind('click', function(event) {
			controller.canvas.showWish(this.rel)
		});
		
		$("#selection-pager-next").bind('click', function(event) {
			
			controller.canvas.listSelectedWishes(data.type, "next");
		});
		
		$("#selection-pager-prev").bind('click', function(event) {
			controller.canvas.listSelectedWishes(data.type, "prev");
		});
		
        $('a.link-wish-copy').bind('click', function() {
        	
    		controller.canvas.showWishCopy(this.rel);
        });
        
        $('a.link-wish-like').bind('click', function() {
        	
        	controller.canvas.likeWish(this.rel);
        });
        
		$('a.link-show-userslist').unbind().bind('click', function(event) {
			controller.canvas.listUserWishes(this.rel)
		});
		
		$('#link-show-ranked').unbind().bind('click', function(event) {
			controller.canvas.listSelectedWishes("ranked")
		});
		
		$("#link-show-newest").bind('click', function(event) {
			controller.canvas.listSelectedWishes("newest");
		})
		
		$("#section-selection").slideDown("slow", gadgets.window.adjustHeight);
	},
	
	renderWish : function(data) {
				
		$("#content").hide();
		
		var html = Mustache.to_html(template.canvas.wish_show, data);
		
		$("#wish-view").html(html);
		
		$("#wish-view").slideDown("slow");
		
		$("button.button-show-back").bind("click", function() {
			$("#wish-view").hide();
			$("#content").slideToggle("slow");
		});
		
        $('a.link-wish-copy').unbind().bind('click', function() {
			$("#wish-view").hide();
			$("#content").slideToggle("slow");
    		controller.canvas.showWishCopy(this.rel);
        });
        
        $('a.link-wish-like').unbind().bind('click', function() {
        	
        	controller.canvas.likeWish(this.rel);
        });
        
		$('a.link-show-userslist').unbind().bind('click', function(event) {
			controller.canvas.listUserWishes(this.rel)
		});
		
		if (data.likes > 0) controller.canvas.listLikers(data.likers);
	},

	renderLikers : function(data)
	{
		var params = {"users" : utils.flatten(data)};

		var html = Mustache.to_html(template.canvas.likers_list, params);

		$("#div-likers-list").html(html);
		
		$(".carousel").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: (params.users.length > 7 ? 7 : params.users.length),
			scroll: (params.users.length > 3 ? 3 : 1),
	        circular: true
		});

		$('a.link-show-userslist').unbind().bind('click', function(event) {
			controller.canvas.listUserWishes(this.rel)
		});

		gadgets.window.adjustHeight(800);
	},

	renderUsersWishes : function(data) {
		
		$("#content").hide();
		
		data.show_prev = data.page > 1;
		data.show_next = data.page < data.pages;
		
		var html = Mustache.to_html(template.canvas.wish_list_user, data);
		
		$("#wish-view").html(html);
		
        $("#user-pager-next").bind('click', function(event) {

        	controller.canvas.listFriendWishes(data.owner_id, "next");
        });
        
        $("#user-pager-prev").bind('click', function(event) {
        	
        	controller.canvas.listFriendWishes(data.owner_id, "prev");
        });
		
        $('a.link-wish-show').unbind().bind('click', function(event) {
        	controller.canvas.showWish(this.rel)
        });
		
		$("#wish-view").slideDown("slow");
		
		$("button.button-show-back").bind("click", function() {
			$("#wish-view").hide();
			$("#content").slideToggle("slow");
		});
		
		gadgets.window.adjustHeight(550);
	},

	renderFriendlist : function(data) {

		var params = {"friends" : utils.flatten(data)};

		var html = Mustache.to_html(template.canvas.friends_list, params);

		$("#friends-list").html(html);
		
		if (params.friends.length) {
			
			$(".carousel").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				visible: (params.friends.length > 4 ? 4.2 : params.friends.length),
				scroll: (params.friends.length > 2 ? 2 : 1),
				circular: true
			});
			
			$("a.friends-list-item").bind("click", function () {
				controller.canvas.listFriendWishes(this.rel);
			});
		}
    	
    	$("a.link-invite").unbind().bind("click", function () {
    		model.request.inviteFriends();
    	});
	}
};